<template>
	<view>
		<!-- 此组件因为版权问题，请关注"前端组件开发"公众号，私信后获得授权 -->
		<view class="cateBox">
			<!-- 左侧列表 -->
			<scroll-view class="cate-left" scroll-y="true" :scroll-top="verticalNavTop" scroll-with-animation>
				<view class="cate-left-list" v-for="(item,index) in data" :key="index" @click="tapselect(index)">
					<!-- activeItem为选中状态 -->
					<view class="cate-left-item " :class="{activeItem:currentTab==index}">
						<label></label><text>{{item.name}}</text>
					</view>
				</view>
			</scroll-view>
			<!-- 右侧列表 -->
			<scroll-view class="cate-right" scroll-y="true" @scroll="mainScroll" :scroll-into-view="'cate-'+mainCur"
				scroll-with-animation>
				<view v-for="(item,index) in data" :key="index" :id="'cate-'+index">
					<view class="cate-right-title">
						<view class="line"></view>
						<text>{{item.name}}</text>
					</view>
					<view class="cate-right-list">
						<view class="cate-right-item" v-for="(child,childindex) in item.articles" :key="childindex"
							@click="itemClick(child)">
							<view class="t1">{{child.name}}</view>
							<view class="flex t2 mb-10">
								<view>{{child.title}}</view>
								<view class="flex">
									<u-icon color="#999" name="eye"></u-icon>
									<view>{{getRandomNum(100,3000,childindex)}}</view>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view style="height: 100px;"></view>
			</scroll-view>
		</view>
	</view>
</template>
<script>
	import {
		getCategoryTree
	} from "@/api/module/article.js"
	export default {
		props: {
			// data: {
			// 	type: Array,
			// 	default () {
			// 		return []
			// 	}
			// }
		},
		data() {
			return {
				currentTab: 0,
				mainCur: 0,
				verticalNavTop: 0,
				list: [{}],
				data: []
			}
		},
		mounted() {},
		methods: {
			getData(tag) {
				const that = this;
				getCategoryTree({
					tag,
					page: 1,
					limit: 100
				}).then(res => {
					that.data = res;
					setTimeout(() => {
						that.getHeight();
					}, 100)
				})
			},
			getRandomNum(Min = 100, Max = 3000, index) {
				const Range = Max - Min + index;
				const Rand = Math.random();
				return Min + Math.floor(Rand * Range);
			},
			itemClick(item) {
				this.$emit('itemClick', item)
			},
			//点击左侧按钮
			tapselect(index) {
				this.mainCur = -1;
				setTimeout(() => {
					this.currentTab = index;
					this.mainCur = index;
					this.verticalNavTop = (index - 1) * 50;
				}, 10)
			},
			//右侧滚动
			mainScroll(e) {
				let scrollTop = e.detail.scrollTop + 10;
				for (let i = 0; i < this.list.length; i++) {
					if (scrollTop > this.list[i].top && scrollTop < this.list[i].bottom) {
						// 此处屏蔽，避免点击最后一个回退， 但是没有右边滚动指示
						if (this.currentTab != this.list.length - 1) {
							this.currentTab = i;
							this.verticalNavTop = (i - 1) * 50
							return false
						}
					}
				}
			},
			//获取右侧每项的top和bottom,里面需获取对应dom元素的高度，所以只能在mounted生命周期里调用
			getHeight() {
				var top = 0;
				var bottom = 0;
				var temp = 0;
				var that = this
				for (let i = 0; i < this.data.length; i++) {
					let view = uni.createSelectorQuery().select("#cate-" + i)
					// #ifdef  MP-WEIXIN
					view = uni.createSelectorQuery().in(that).select("#cate-" + i);
					//	#endif
					view.fields({
						size: true
					}, data => {
						top = temp;
						bottom = temp + data.height;
						that.list[i] = {
							top: top,
							bottom: bottom
						};
						temp = bottom;
					}).exec();
				}
			}
		}
	}
</script>
<style scoped lang="scss">
	.cateBox {
		position: absolute;
		top: 0rpx;
		bottom: 110rpx;
		background: #fff;
		width: 100%;
		display: flex;
	}

	.cate-left {
		width: 200rpx;
		background: #f4f4f4;
		height: 100%;
	}

	.cate-left-item {
		background: #f4f4f4;
		height: 50px;
		display: flex;
		align-items: center;
	}

	.cate-left-item.activeItem {
		background: #fff;
	}

	.cate-left-item label {
		width: 4rpx;
		height: 15px;
		float: left;
		background: #f4f4f4;
	}

	.cate-left-item.activeItem label {
		background: #0bbbef;
	}

	.cate-left-item text {
		width: 196rpx;
		font-size: 28rpx;
		display: block;
		text-align: center;
	}

	/* 右侧*/
	.cate-right {
		flex: 1;
	}

	.cate-right-title {
		text-align: center;
		font-size: 28rpx;
		margin-top: 20rpx;
		line-height: 80rpx;
		position: relative;
	}

	.cate-right-title .line {
		position: absolute;
		height: 2rpx;
		width: 300rpx;
		background: orange;
		top: 39rpx;
		left: 50%;
		margin-left: -150rpx;
	}

	.cate-right-title text {
		background: #fff;
		padding: 0 10px;
		position: relative;
		color: #999;
	}

	.cate-right-list {
		height: auto;
		overflow: hidden;
	}

	.cate-right-item {
		padding: 10rpx 30rpx;
		width: 100%;
		float: left;
		margin-top: 20rpx;

		.t1 {
			font-size: 30rpx;
			color: #333;
			font-weight: bold;
		}

		.t2 {
			justify-content: space-between;
			font-size: 26rpx;
			color: #999;
			margin-top: 10rpx;
			padding-bottom: 30rpx;
			border-bottom: 1rpx solid #eee;
		}
	}

	.cate-right-item image {
		width: 160rpx;
		height: 160rpx;
		display: block;
		margin: 0 auto;
	}

	.cate-right-item text {
		display: block;
		text-align: center;
		font-size: 24rpx;
	}
</style>